package q503_nextGreaterElements;

import java.util.Arrays;
import java.util.Deque;
import java.util.LinkedList;

public class Solution {
    /*
    通过将数组拼在一起
    然后使用单调栈即可解决
    注意单调栈的模版——找右边大于它的数字 要从右向左

     */
    public int[] nextGreaterElements(int[] nums) {
        int[] res = new int[nums.length];
        Deque<Integer> stack = new LinkedList<>();
        for (int i = 2 * nums.length - 1; i >= 0; i--) {
            while (!stack.isEmpty() && nums[i % nums.length] >= nums[stack.peek()]) {
                stack.pop();
            }
            res[i % nums.length] = stack.isEmpty() ? -1 : nums[stack.peek()];
            stack.push(i % nums.length);
        }

        return res;
    }

    public static void main(String[] args) {
        int[] a = {1, 2, 3};
        System.out.println(Arrays.toString(new Solution().nextGreaterElements(a)));
    }
}
